package com.guaoran.micro.services.spring.cloud.sleuth.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author : gucheng
 * @Description : <br/>
 * @date :2018/12/8 17:36
 */
@RestController
public class DemoController {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private final RestTemplate restTemplate;

    @Autowired
    public DemoController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("")
    public String index() {
        String returnValue = "Hello,World";
        logger.info("{} index() : {}", getClass().getSimpleName(), returnValue);
        return returnValue;
    }


    /**
     * 完整的调用链路：
     * spring-cloud-sleuth
     * -> zuul
     * -> person-service-consumer
     * -> person-service-provider
     *
     * @return
     */
    @GetMapping("/to/zuul/spring-cloud-client/say")
    public String toZuul(@RequestParam String message) {
        logger.info("spring-cloud-sleuth#toZuul()");
        // spring-cloud-zuul :  6060
        String url = "http://spring-cloud-zuul/spring-cloud-client/say?message="+message;
        return restTemplate.getForObject(url, String.class);
    }
}
